package mywebapp.tools.struts;

import mywebapp.impl.*;
import mywebapp.webpage.tools.struts.*;
import mywebapp.dao.*;
import mywebapp.webpage.*;
import java.util.*;

public class STRUTSCONIG_GENERATION_JSP
{
  protected static String nl;
  public static synchronized STRUTSCONIG_GENERATION_JSP create(String lineSeparator)
  {
    nl = lineSeparator;
    STRUTSCONIG_GENERATION_JSP result = new STRUTSCONIG_GENERATION_JSP();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = NL + "\t";
  protected final String TEXT_2 = "<form-beans>";
  protected final String TEXT_3 = NL + "\t\t";
  protected final String TEXT_4 = NL + "\t\t\t<form-bean name=\"";
  protected final String TEXT_5 = "\" type=\"org.";
  protected final String TEXT_6 = "\" />" + NL + "\t\t\t";
  protected final String TEXT_7 = NL + "\t\t";
  protected final String TEXT_8 = NL + "\t";
  protected final String TEXT_9 = NL + "\t</form-beans>" + NL + "\t" + NL + "\t" + NL + "\t<action-mappings>" + NL + "\t";
  protected final String TEXT_10 = NL + "\t\t\t<action path=\"/";
  protected final String TEXT_11 = "\" type=\"org.servletAction.";
  protected final String TEXT_12 = "\"" + NL + "\t\t\t\t\tname=\"";
  protected final String TEXT_13 = "ValidationForm\" scope=\"request\" " + NL + "\t\t\t\t\tinput=\"/";
  protected final String TEXT_14 = ".jsp\">" + NL + "\t\t\t\t<forward name=\"errors\" path=\"/pages/errors.jsp\" />" + NL + "\t\t\t\t\t";
  protected final String TEXT_15 = NL + "\t\t\t\t<forward name=\"success\" path=\"/pages/";
  protected final String TEXT_16 = ".jsp\"/>" + NL + "\t\t\t</action>" + NL + "\t\t\t" + NL + "\t\t";
  protected final String TEXT_17 = NL + "\t";
  protected final String TEXT_18 = NL + "\t" + NL + "\t" + NL + "\t</action-mappings>" + NL + "\t<message-resources parameter=\"Resources\" />" + NL + "</struts-config>";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
     WebAppImpl app = (WebAppImpl) argument; 
    stringBuffer.append(TEXT_1);
    
	String nomAction="";
	String nomForm="";
	List<Page> pages = app.getPages();
	ArrayList<Form> forms = new ArrayList<Form>();
	
    stringBuffer.append(TEXT_2);
    
	for(Page p : pages){
		for (Object b : p.getBalises()){
			if (b instanceof Form && !forms.contains(b))
				forms.add((Form) b);
		}
    stringBuffer.append(TEXT_3);
    for (Form f : forms){
			if (f.getAction().equals(EActionForm.ADD_LITERAL) || f.getAction().equals(EActionForm.UPDATE_LITERAL))
			{ 
			  nomForm = (XMLGenrator.makeFirstMaj(f.getName())+ XMLGenrator.makeFirstMaj(((Entity)f.getEntities().get(0)).getName())+"ValidationForm").replaceAll(" ", "");
    stringBuffer.append(TEXT_4);
    stringBuffer.append(nomForm);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(("actionForm"+nomForm).replaceAll(" ", ""));
    stringBuffer.append(TEXT_6);
    }
    stringBuffer.append(TEXT_7);
    }
    stringBuffer.append(TEXT_8);
    }
    stringBuffer.append(TEXT_9);
    
	for(Page p : pages){
		for (Object b : p.getBalises()){
			if (b instanceof Form && !forms.contains(b))
				forms.add((Form) b);
		}
		for (Form f : forms){
			  nomForm = (XMLGenrator.makeFirstMaj(f.getName())+ XMLGenrator.makeFirstMaj(((Entity)f.getEntities().get(0)).getName())).replaceAll(" ", "");
    stringBuffer.append(TEXT_10);
    stringBuffer.append(nomForm);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(nomForm);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(nomForm);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(nomForm.toLowerCase());
    stringBuffer.append(TEXT_14);
    String next =f.getNext().getName().toLowerCase(); 
    stringBuffer.append(TEXT_15);
    stringBuffer.append(next);
    stringBuffer.append(TEXT_16);
    }
    stringBuffer.append(TEXT_17);
    }
    stringBuffer.append(TEXT_18);
    return stringBuffer.toString();
  }
}
